module DebugHelper
  LOG_PATH = "debug.log"

  def self.init
    begin
      File.open(LOG_PATH, "wb") { |f| f.write("\xEF\xBB\xBF") }  # 写入 UTF-8 BOM
    rescue
    end
  end

  def self.log(msg)
    begin
      # ✅ 手动格式化时间，避免 “中国标准时间” 乱码
      timestamp = Time.now.strftime("%Y-%m-%d %H:%M:%S")

      text = "[#{timestamp}] #{msg}\n"

      if defined?(Kconv)
        require 'kconv'
        text = text.kconv(Kconv::UTF8, Kconv::AUTO)
      end

      File.open(LOG_PATH, "ab") { |f| f.write(text) }
    rescue => e
    end
  end
end

DebugHelper.init